package com.webSite.demo.cy.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.code.kaptcha.Constants;
import com.webSite.demo.cy.bean.HomeAdmin;
import com.webSite.demo.cy.utils.FileUtils;
import com.webSite.demo.cy.utils.JsonUtils;


/**
 * 没有用到的页面链接
* @ClassName: AdminController  
* @author yanfa00027  
* @date 2018年5月16日  
*
 */
@Controller
@EnableAutoConfiguration
public class AdminController {
	
	@RequestMapping("/admin.html")  
    String admin() {
        return "admin/index";  
    }
	
	@RequestMapping("/add.html")  
    String add() {  
        return "admin/add";  
    }
	
	@RequestMapping("/adv.html")  
    String adv() {  
        return "admin/adv";  
    }
	
	@RequestMapping("/cate.html")  
    String cate() {  
        return "admin/cate";  
    }
	
	@RequestMapping("/cateedit.html")  
    String cateedit() {  
        return "admin/cateedit";  
    }
	
	@RequestMapping("/column.html")  
    String column() {  
        return "admin/column";  
    }
	
	@RequestMapping("/info.html")  
    String info(Model model) {  
        return "admin/info";  
    }
	
	@RequestMapping("/list.html")  
    String list() {  
        return "admin/list";  
    }
	
	@RequestMapping("/login.html")  
    String login() {  
        return "admin/login";
    }
	
	@RequestMapping("/pass.html")  
    String pass() {  
        return "admin/pass";  
    }
	
	@RequestMapping("/tips.html")  
    String tips() {  
        return "admin/tips";  
    }
	
	@RequestMapping(value="/pic/upload", produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")
	@ResponseBody
	public String uploadFile(HttpServletRequest request,HttpServletResponse response,MultipartFile uploadFile) {
		try {
			//封装到map中返回
			FileUtils fileUtils =new FileUtils();
			String urlString=fileUtils.uploadToInputStream(uploadFile.getOriginalFilename(),uploadFile.getInputStream());
			Map<Object, Object> result = new HashMap<Object, Object>();
			result.put("error", 0);
			result.put("url", urlString);
			return JsonUtils.objectToJson(result);
		} catch (Exception e) {
			e.printStackTrace();
			Map<Object, Object> result = new HashMap<Object, Object>();
			result.put("error", 1);
			result.put("message", "图片上传失败");
			return JsonUtils.objectToJson(result);
		}
	}
	@RequestMapping("/login-admin.html")
	@ResponseBody
	public Map<String, Object> getInfo(HttpServletRequest request,HomeAdmin admin,RedirectAttributes model) {
		Map<String, Object> map = new HashMap<String,Object>();
		String verifyCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		if(!admin.getUsername().equalsIgnoreCase("admin")) {
			map.put("msg","用户名不正确");
			map.put("state", "error");
		}else if(!admin.getPassword().equalsIgnoreCase("admin")) {
			map.put("msg","密码不正确");
			map.put("state", "error");
		}else if(!admin.getCode().equalsIgnoreCase(verifyCode)) {
			map.put("msg","验证码错误");
			map.put("state", "error");
		}else {
			map.put("msg","登陆成功");
			map.put("state", "success");
		}
		return map;
		
	}
	
	@RequestMapping("/test.html")
	public String test() {
		return "test";
	}
	
	@GetMapping("/test")
	public String adtest() {
		return "test";
	}
	
	@PostMapping("/test")
	@ResponseBody
	public Map<String, Object> upTest(HttpServletRequest request,HomeAdmin admin) {
		String verifyCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		System.out.println("code=="+verifyCode);
		Map<String, Object> map = new HashMap<String,Object>();
		System.out.println("user="+admin);
		map.put("msg", "登录成功");
		return map;
	}
}
